<?php

namespace app\applet\controller\user;

use app\applet\model\user\User;
use app\common\controller\Applet;

use app\common\model\work_area\WorkAreaUser;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("用户相关详情")
 */
class Info extends Applet
{
    /**
     * @Apidoc\Title("用户详情")
     * @Apidoc\Url("/applet/user.Info/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("id", type="int", desc="用户id"),
     *    @Apidoc\Returned("nickname", type="string", desc="昵称"),
     *    @Apidoc\Returned("phone", type="int", desc="手机号"),
     *    @Apidoc\Returned("head", type="string", desc="头像"),
     *    @Apidoc\Returned("work_area_user_id", type="string", desc="0未绑定"),
     *    @Apidoc\Returned("type", type="int", desc="1总部 2工区账号 3站点"),
     *    @Apidoc\Returned("work_area_id", type="int", desc="type=2 才有,工区id"),
     *    @Apidoc\Returned("work_area_stand_id", type="int", desc="type=3 才有,站点id"),
     * )
     */
    public function info()
    {
        try {
            $result = JWTAuth::auth();
            $user_id = $result['user_id'];
            $work_area_user_id = $result['work_area_user_id'];
            $where = ['id' => $user_id];
            if ($work_area_user_id > 0) {
                $where = ['work_area_user_id' => $work_area_user_id];
            }
            $info = User::where($where)->field(['id', 'nickname', 'phone', 'head', 'work_area_user_id'])->find();
            if (!empty($info)) {
                $info = $info->toArray();
            }
            $info['type'] = 0;
            $info['work_area_id'] = 0;
            $info['work_area_stand_id'] = 0;
            //已绑定工区
            if ($info['work_area_user_id'] > 0) {
                //工区账号
                $work_area_user = WorkAreaUser::where(['id' => $info['work_area_user_id']])->find();
                $info['type'] = $work_area_user->type ?? 0;
                $info['work_area_id'] = $work_area_user->work_area_id ?? 0;
                $info['work_area_stand_id'] = $work_area_user->work_area_stand_id ?? 0;
                $info['account_name'] = '中铁城投轨道AI预警平台';
                $info['account_logo'] = 'https://aiscreen.smartrail.cloud/upload/20231213/13336.png';
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, "获取成功", $info);
    }
}